package com.east.study;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.content.Context;
import android.os.Environment;

public class VideoDec {

	//private static final String DOWNLOAD_DIRECTOTY = "eastdownloads";
	private final String videoFileName="qpad.mp4";
	private final String encFileName="mynec.dat";
	//private Context contxt;
	private Cipher dec;
	
	public VideoDec(Context context)
	{
		    //contxt = context;
	        byte[] key = null;
			try {
				key = "0123456789abcdef".getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	        // iv
	        byte[] iv = null;
			try {
				iv = "fedcba9876543210".getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

	        
	        SecretKeySpec keySpec;
	        IvParameterSpec ivSpec;
	        keySpec = new SecretKeySpec(key, "AES");
	        ivSpec = new IvParameterSpec(iv);
	        try {
				dec = Cipher.getInstance("AES/CBC/PKCS5Padding");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				e.printStackTrace();
			}
	        try {
				dec.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
			} catch (InvalidKeyException e) {
				e.printStackTrace();
			} catch (InvalidAlgorithmParameterException e) {
				e.printStackTrace();
			}
	}
	private boolean fileIsExists(String OrigFile)
	{
	        try{
	                File f=new File(OrigFile);
	                if(!f.exists()){
	                        return false;
	                }
	                
	        }catch (Exception e) {
	                // TODO: handle exception
	                return false;
	        }
	        return true;
	}
		
	public String DecryptVideo(String OrigFile)
	{
		FileOutputStream fos = null;
		
		if (!fileIsExists(OrigFile))
			return "";
		
		String strV=Environment.getExternalStorageDirectory().getPath()+File.separator+videoFileName;
		String strEnc=Environment.getExternalStorageDirectory().getPath()+File.separator+encFileName ;
		try {
			
			//fos = contxt.openFileOutput(strV,Context.MODE_PRIVATE);
			 fos = new FileOutputStream(strV);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		FileOutputStream osEnc=null;
		File encFile = new File(strEnc);
	    try {
			osEnc = new FileOutputStream(encFile);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		FileInputStream fis=null;
		try {
			fis = new FileInputStream(OrigFile);
			byte[] readBuffer = new byte[1168];
			try {
				fis.read(readBuffer);
				osEnc.write(readBuffer);
				osEnc.close();
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		FileInputStream inEnc = null;
		try {
			inEnc = new FileInputStream(encFile);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		if (fos != null)
		{
		        CipherInputStream cins = new CipherInputStream(inEnc,dec);
		        
				 byte[] tempbuffer = new byte[2048];
		           try {
					for(int n =0;(n=cins.read(tempbuffer))!=-1;){
					    fos.write(tempbuffer,0,n);
					   }
				} catch (IOException e) {
					e.printStackTrace();
				};
		
			    try {
			    	
			    	//BufferedOutputStream
			    	byte[] buffer = new byte[2048*1024];
			    	for(int n =0;(n=fis.read(buffer))!=-1;){
		   			    fos.write(buffer,0,n);
		   			} 
			    	fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		           try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		           try {
					cins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return strV;
	}
}
